package com.bigflexlib.ds.op
{
	
	import flash.events.IEventDispatcher;
	
	import com.bigflexlib.ds.DataStructureEvent;
	import com.bigflexlib.ds.container.IRContainer;
	import com.bigflexlib.utils.IOperator;
	import com.bigflexlib.x.flash.events.EventListenerSpec;


	public class AbstractContainerOp implements IOperator
	{
		

		private var iContainerELS:EventListenerSpec = new EventListenerSpec( DataStructureEvent.CHANGE, iContainer_change );


		protected var iContainer:IRContainer;
		
		

		private var internalOperators:Array = [];
		
		

		public function AbstractContainerOp( iContainer:IRContainer )
		{

			this.iContainer = iContainer;
			
			if ( iContainer is IEventDispatcher )
				iContainerELS.addToDispatcher( iContainer as IEventDispatcher );

		}


		// abstract
		protected function iContainer_change( event:DataStructureEvent  ):void
		{
		
		
		}


		
		/**
		 * 
		 * A conventional way to store operators that need to be dispose()d when this operator dies
		 * 
		 * @param op
		 * 
		 */		
		protected function registerInternalOperator( op:IOperator ):void
		{
			internalOperators.push( op );
		}


		
		public function dispose( ):void
		{
		
			for each ( var op:IOperator in internalOperators )
			{
				op.dispose( );
			}
			
		
			if ( iContainer is IEventDispatcher )
				iContainerELS.removeFromDispatcher( iContainer as IEventDispatcher );
			
			iContainer = null;
		
		}
		
		
		
	}
}